package com.benmei.weike.service;

import com.benmei.weike.common.Constants;
import com.benmei.weike.dao.ClassRoomDao;
import com.benmei.weike.dao.DiscountCouponDao;
import com.benmei.weike.dao.StudentDao;
import com.benmei.weike.dto.PageRequest;
import com.benmei.weike.dto.PageResponse;
import com.benmei.weike.dto.mcCourse.McCoursewareDto;
import com.benmei.weike.dto.otherplatform.LoginDto;
import com.benmei.weike.entity.ClassRoom;
import com.benmei.weike.entity.DiscountCoupon;
import com.benmei.weike.entity.Student;
import com.benmei.weike.exception.ClientException;
import com.benmei.weike.util.DateUtil;
import com.benmei.weike.web.vo.my.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by Peter on 2018/4/14.
 */
@Component
public class StudentService {

    @Autowired
    private StudentDao studentDao;
    @Autowired
    private ClassRoomDao classRoomDao;
    @Autowired
    private DiscountCouponDao discountCouponDao;

    // 第三方平台的注册或登录
    public Student loginOrRegiest(LoginDto loginDto) {
        String uid = loginDto.getUid();
        Student student = studentDao.getByIncourseUid(uid);
        // 用户不存在，新注册
        if (student == null) {
            student = new Student();
            student.setToken(UUID.randomUUID().toString());
            student.setIncourse_uid(uid);
            student.setMemb_name(uid);
            Date now = new Date();
            String yearMonth = DateUtil.format(now, "yyyyMM");
            student.setMemb_register_time(now);
            student.setMemb_register_month(yearMonth);
            student.setTotal_study_times(0);
            student.setMemb_balance(new BigDecimal(0));
            student.setMemb_phone_area(loginDto.getPhone_area());
            student.setMemb_phone(loginDto.getPhone());
            student.setMemb_islock(Constants.Member.IsLock.unLocked);//0锁定1未锁定  只有为1时才能登陆
            student.setUid(UUID.randomUUID().toString());
            student.setMemb_head_portrait("http://file.waijiaojun.com/waijiaojun/app/image/head_InCourse.png");
            studentDao.insertSelective(student);

            // 2.添加到优惠券表中
            DiscountCoupon discountCoupon = new DiscountCoupon();
            discountCoupon.setCourse_type(1);
            discountCoupon.setMemb_id(student.getMemb_id());
            discountCoupon.setDeleted(0);
            discountCoupon.setOver((double) 0);
            discountCoupon.setSubtract((double) 120);
            discountCoupon.setDiscount_state(0);
            discountCouponDao.insertDiscountCoupon(discountCoupon);
        } else {
            // 更新或添加电话号码
            if (StringUtils.isNotBlank(loginDto.getPhone())) {
                student.setMemb_phone(loginDto.getPhone());
                student.setMemb_phone_area(loginDto.getPhone_area());
                studentDao.update(student);
            }
        }

        return student;
    }

    // 查询上课列表
    public PageResponse<ReserveVo> findReserve(PageRequest pageRequest, Integer memb_id) {
        PageResponse<ReserveVo> pageResponse = new PageResponse(pageRequest.getPageSize(), pageRequest.getCurrentPage());

        Long total = studentDao.findReserveCount(memb_id);
        pageResponse.setTotal(total);

        RowBounds rowBounds = new RowBounds(pageResponse.getOffset().intValue(), pageResponse.getPageSize().intValue());
        List<ReserveVo> list = studentDao.findReserve(memb_id, rowBounds);

        pageResponse.setData(list);


        return pageResponse;
    }

    public RecordingDetailResponse findRecordingDetail(RecordingDetailRequest request, Integer memb_id) {
        RecordingDetailResponse response = new RecordingDetailResponse();
        // 查询这堂课的录音列表
        List<RecordingVo> recordingList = studentDao.findRecordingDetail(request.getRoom_id());

        // 查询这堂课的ppt列表
        List<McCoursewareDto> coursewareList = null;
        // 查询cou_id
        ClassRoom classRoom = classRoomDao.findById(request.getRoom_id());
        if (classRoom == null) {
            throw new ClientException("无效的roomId：" + request.getRoom_id());
        }

        // 查询 start_page 和 end_page
        ClassRoomPageVo classRoomPageVo = classRoomDao.findPageRangeByRoomId(request.getRoom_id());
        if (classRoomPageVo == null || classRoomPageVo.getStart_page() == null || classRoomPageVo.getEnd_page() == null) {
            coursewareList = new ArrayList<>();
        } else {
            coursewareList = classRoomDao.findPptsForClassRoom(classRoom.getCou_id(), classRoomPageVo.getStart_page(), classRoomPageVo.getEnd_page());
        }

        response.setCoursewareList(coursewareList);
        response.setRecordingList(recordingList);

        return response;
    }
}
